package com.ease.map.handler.feature.draw
{
	import com.ease.map.Map;
	import com.ease.map.cursor.CrossCursor;
	import com.ease.map.cursor.SnapLineCursor;
	import com.ease.map.cursor.SnapVertexCursor;
	import com.ease.map.events.DrawEvent;
	import com.ease.map.feature.Feature;
	import com.ease.map.geometry.PointGeometry;
	import com.ease.map.geometry.basetype.Extent;
	import com.ease.map.geometry.basetype.Location;
	import com.ease.map.geometry.basetype.Pixel;
	import com.ease.map.handler.Handler;
	import com.ease.map.handler.feature.draw.snap.SnapReturn;
	import com.ease.map.handler.feature.draw.snap.SnapTypeEnum;
	import com.ease.map.layer.FeatureLayer;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;

	/**
	 * Handler base class for Drawing Handler
	 */
	public class AbstractDrawHandler extends Handler
	{
		/**
		 * The layer concerned by the drawing Operation
		 * */
		private var _drawLayer:FeatureLayer;
		//可捕捉的图层ids
		protected var _snapLayerIds:ArrayCollection;
		protected var _snapLayers:Vector.<FeatureLayer>;
		//容差默认6个像素为单位，
		private var _snapTolerance:Number=6;
		protected var snapReturn:SnapReturn;
		private var _snapClickCallback:Function=null;
		private var _startDrawCallback:Function=null;
		private var _stopDrawCallback:Function=null;
		private var _backToPreviouCallback:Function=null;
		protected var _dbclickEnabled:Boolean=true;

		public function get dbclickEnabled():Boolean
		{
			return _dbclickEnabled;
		}

		public function set dbclickEnabled(value:Boolean):void
		{
			_dbclickEnabled=value;
		}


		/**
		 * 回退back
		 * */
		public function get backToPreviouCallback():Function
		{
			return _backToPreviouCallback;
		}

		/**
		 * @private
		 */
		public function set backToPreviouCallback(value:Function):void
		{
			_backToPreviouCallback=value;
		}


		/**
		 * 捕获设施的回调函数，抛出捕捉到的feature，及捕获点坐标
		 *
		 * */
		public function get snapClickCallback():Function
		{
			return _snapClickCallback;
		}

		public function set snapClickCallback(value:Function):void
		{
			_snapClickCallback=value;
		}

		/**
		 * Constructor
		 * @param map Map Object
		 * Abstract class never use this constructor
		 * */
		public function AbstractDrawHandler(map:Map, handlername:String)
		{
			//map.operateMode=MapActionEnum.MAP_DRAW;
			super(map, handlername);
		}

		/**
		 * 画起始点，由外部点击起始点通知开始画，不需要再鼠标按下开始画
		 * */
		protected function drawStartPoint(evt:DrawEvent):void
		{

		}

		/**
		 * 画终止点，直接将最后的点传入，结束绘制
		 * */
		protected function drawEndPoint(evt:DrawEvent):void
		{

		}

		public function get snapTolerance():Number
		{
			return _snapTolerance;
		}

		/**
		 * 捕捉容差，以像素为单位，建议不要设置采用默认容差值
		 * */
		public function set snapTolerance(value:Number):void
		{
			_snapTolerance=value;
		}

		public function get snapLayers():Vector.<FeatureLayer>
		{
			if (_snapLayers == null)
			{
				_snapLayers=new Vector.<FeatureLayer>();
			}
			return _snapLayers;
		}

		public function set snapLayers(value:Vector.<FeatureLayer>):void
		{
			_snapLayers=value;
		}

		public function get snapLayerIds():ArrayCollection
		{
			return _snapLayerIds;
		}

		public function set snapLayerIds(value:ArrayCollection):void
		{
			_snapLayerIds=value;
			if (value != null && value.length > 0)
			{
				for each (var layerId:int in _snapLayerIds)
				{
					if (map.getLayerById(layerId) != null)
					{
						snapLayers.push(map.getLayerById(layerId) as FeatureLayer);
					}

				}
			}
		}

		override public function set map(value:Map):void
		{
			if (value != null)
			{
				super.map=value;
			}
		}

		/**
		 * The layer concerned by the drawing Operation
		 * */
		public function get drawLayer():FeatureLayer
		{
			if (_drawLayer == null)
			{
				_drawLayer=new FeatureLayer(-1, "constructor");
			}
			return _drawLayer;
		}

		/**
		 * @private
		 * */
		public function set drawLayer(value:FeatureLayer):void
		{
			_drawLayer=value;
		}

		override public function initialize():void
		{
			snapLayers=null;
		}

		protected function mouseMoveSnapHandler(event:MouseEvent):void
		{
			var flag:int=-1;
			if (!snapLayers || snapLayers.length == 0){
				return;
			}
			var pixel:Pixel=new Pixel(drawLayer.mouseX, drawLayer.mouseY);
			var lonlat:Location=this.map.getLocationFromLayerPx(pixel);
			var mousePoint:PointGeometry=new PointGeometry(lonlat.x, lonlat.y);
			var distance:Number=map.resolution * snapTolerance;
			var snapPoint:PointGeometry=new PointGeometry;
			snapReturn=new SnapReturn();
			map.clearHighLight();
			var originExtent:Extent;
			//			var starttime:Date = new Date();
			for each (var layer:FeatureLayer in snapLayers){
				originExtent=new Extent(mousePoint.x - distance, mousePoint.y - distance, mousePoint.x + distance, mousePoint.y + distance);
				for (var i:int=0; i < layer.features.length; i++)
				{
					var feature:Feature=layer.features[i];
					if (feature.geometry.getExtent().intersectsBounds(originExtent, true))
					{
						//计算点与feature各个节点的距离
						snapReturn.snapType=feature.geometry.getTolerancePoint(mousePoint, distance, snapPoint);
						if (snapReturn.snapType != SnapTypeEnum.NO_SNAPS){
							snapReturn.snapPoint=snapPoint;
							snapReturn.snapFeature=feature;
							snapReturn.snapFeatures.addItem(feature);
							//设置高亮绿色，表明被捕捉设施。
							feature.highLighted=true;
						}
					}
				}
			}
			switch (snapReturn.snapType)
			{
				case SnapTypeEnum.SNAP_OVERTEX:
					if (flag != SnapTypeEnum.SNAP_OVERTEX)
					{
						CursorManager.removeAllCursors();
						CursorManager.setCursor(SnapVertexCursor);
						flag=SnapTypeEnum.SNAP_OVERTEX
					}
					break;
				case SnapTypeEnum.SNAP_INLINE:
					if (flag != SnapTypeEnum.SNAP_INLINE)
					{
						CursorManager.removeAllCursors();
						CursorManager.setCursor(SnapLineCursor);
						flag=SnapTypeEnum.SNAP_INLINE
					}
					break;
				case SnapTypeEnum.NO_SNAPS:
					if (flag != SnapTypeEnum.NO_SNAPS)
					{
						CursorManager.removeAllCursors();
						CursorManager.setCursor(CrossCursor);
						flag=SnapTypeEnum.NO_SNAPS
					}
					snapReturn=null;
			}
		}
	}
}

